package xin.petstore.framework.cas.session.redis;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

public class RedisCache<K, V>
        implements Cache<K, V> {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private RedisSerializer<Object> serializer = new JdkSerializationRedisSerializer();
    private RedisManager cache;
    private String keyPrefix = "shiro_redis_session:";

    public String getKeyPrefix() {
        return this.keyPrefix;
    }

    public void setKeyPrefix(String keyPrefix) {
        this.keyPrefix = keyPrefix;
    }

    public void setSerializer(RedisSerializer<Object> serializer) {
        this.serializer = serializer;
    }

    public RedisCache(RedisManager cache) {
        if (cache == null) {
            throw new IllegalArgumentException("Cache argument cannot be null.");
        }
        this.cache = cache;
    }

    public RedisCache(RedisManager cache, String prefix) {
        this(cache);

        this.keyPrefix = prefix;
    }

    private byte[] getByteKey(K key) {
        if ((key instanceof String)) {
            String preKey = this.keyPrefix + key;
            return preKey.getBytes();
        }
        return this.serializer.serialize(key);
    }

    public V get(K key)
            throws CacheException {
        if (this.logger.isDebugEnabled())
            this.logger.debug("根据key从Redis中获取对象 key [" + key + "]");
        try {
            if (key == null) {
                return null;
            }
            byte[] rawValue = this.cache.get(getByteKey(key));

            return this.serializer.deserialize(rawValue);
            ;
        } catch (Throwable t) {
            throw new CacheException(t);
        }

    }

    public V put(K key, V value)
            throws CacheException {
        if (this.logger.isDebugEnabled())
            this.logger.debug("根据key从存储 key [" + key + "]");
        try {
            this.cache.set(getByteKey(key), this.serializer.serialize(value));
            return value;
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    public V remove(K key)
            throws CacheException {
        if (this.logger.isDebugEnabled())
            this.logger.debug("从redis中删除 key [" + key + "]");
        try {
            Object previous = get(key);
            this.cache.del(getByteKey(key));
            return previous;
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    public void clear()
            throws CacheException {
        if (this.logger.isDebugEnabled())
            this.logger.debug("从redis中删除所有元素");
        try {
            this.cache.flushDB();
        } catch (Throwable t) {
            throw new CacheException(t);
        }
    }

    public int size() {
        try {
            Long longSize = new Long(this.cache.dbSize().longValue());
            return longSize.intValue();
        } catch (Throwable t) {
        }
        throw new CacheException(t);
    }

    public Set<K> keys() {
        try {
            Set keys = this.cache.keys(this.keyPrefix + "*");
            if (CollectionUtils.isEmpty(keys)) {
                return Collections.emptySet();
            }
            Set newKeys = new HashSet();
            for (byte[] key : keys) {
                newKeys.add(key);
            }
            return newKeys;
        } catch (Throwable t) {
        }
        throw new CacheException(t);
    }

    public Collection<V> values() {
        try {
            Set keys = this.cache.keys(this.keyPrefix + "*");
            if (!CollectionUtils.isEmpty(keys)) {
                List values = new ArrayList(keys.size());
                for (byte[] key : keys) {
                    Object value = get(key);
                    if (value != null) {
                        values.add(value);
                    }
                }
                return Collections.unmodifiableList(values);
            }
            return Collections.emptyList();
        } catch (Throwable t) {
        }
        throw new CacheException(t);
    }
}

/* Location:           C:\java代码反编译器\core-1.1.4.jar
 * Qualified Name:     com.eloancn.framework.cas.session.redis.RedisCache
 * JD-Core Version:    0.6.0
 */